package com.eqdd.yiqidian.ui.mine.wo;

import android.databinding.DataBindingUtil;
import android.view.View;

import com.eqdd.inputs.AndroidNextInputs;
import com.eqdd.inputs.LazyLoaders;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.nextinputs.ValueScheme;
import com.eqdd.yiqidian.PassWordUpdateActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.MainActivity;
import com.eqdd.yiqidian.utils.EncryptUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

public class PassWordUpdateActivity extends BaseActivity {
    private PassWordUpdateActivityCustom dataBinding;
    private AndroidNextInputs nextInputs;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_pass_word_update);
        initTopTitleBar(View.VISIBLE, "修改密码");
        nextInputs = new AndroidNextInputs();
        nextInputs.add(dataBinding.etOldPass, StaticScheme.Required())
                .add(dataBinding.etNewPass, StaticScheme.Required());
        // 必选，与邮件相同
        final LazyLoaders loader = new LazyLoaders(this);
        nextInputs.add(dataBinding.etNewPassAgain, ValueScheme.Required(), ValueScheme.EqualsTo(loader.fromEditText(R.id.et_new_pass)));
    }

    @Override
    public void initData() {

    }

    @Override
    public void setView() {


    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_update_pass:
                if (nextInputs.test()) {
                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_PASS)
                            .params("userGuid", user.getGuid())
                            .params("oldPassword", EncryptUtil.sha1(dataBinding.etOldPass.getText().toString().trim()+"EQD"))
                            .params("newPassword", EncryptUtil.sha1(dataBinding.etNewPass.getText().toString().trim()+"EQD"))
                            .execute(new DialogCallBack<HttpResult>(this) {
                                @Override
                                public void onSuccess(Response<HttpResult> response) {
                                    HttpResult httpResult = response.body();
                                    ToastUtil.showShort(httpResult.getMsg());
                                    if (httpResult.getStatus() == 200) {
                                        startActivity(MainActivity.class);
                                    }
                                }
                            });
                }
        }
    }
}
